package wymiana.service;

import java.util.List;

import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import wymiana.dao.DepartmentDao;
import wymiana.domain.Department;

/**
 * 
 * @author Rafal Magdon
 * @version 1.0
 * 
 *          Description:
 * 
 */
@Service("departmentService")
@Transactional
public class DepartmentService {
	@Autowired
	private DepartmentDao departmentDao;

	public void create(Department d) {
		departmentDao.save(d);

	}

	public List<Department> getAllDepartments() {
		return departmentDao.getAll();
	}

	public Department getDepartment(int p_id) {
		Department d = departmentDao.getDepartmentById(p_id);
		try {
			Hibernate.initialize(d.getSchool());
			Hibernate.initialize(d.getdGroups());
		} catch (Exception e) {

		}

		return d;
	}

	public void updateDepartment(Department d) {
		departmentDao.merge(d);
		// departmentDao.update(d);
	}

	public void deleteDepartmentById(int id) {
		Department d = departmentDao.getDepartmentById(id);
		departmentDao.delete(d);
	}
}
